using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.Utilities.Encoders;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Org.BouncyCastle.OpenSsl;

namespace MiMiPC
{
    internal static class Program
    {
        [STAThread]
        static void Main()
        {
           // var mypwd = "zsapp221133".ToCharArray();
      
             string base64String = File.ReadAllText("./zsapp.keystore");
            byte[] decode = Convert.FromBase64String(base64String);

             MemoryStream stream = new MemoryStream(decode); // 注意释放

            Pkcs12Store pkcs12Store = new Pkcs12Store();
            pkcs12Store.Load(stream,new char[]{} );
            string alias = "zsapp";
            //拿到别名
            var var3 = pkcs12Store.Aliases;
            foreach (var item in var3)
            {
                alias = item.ToString(); //一般只有一个                                   
            }

            #region 从X509CertificateEntry中拿公钥

            AsymmetricKeyParameter publicKey = null;
            X509CertificateEntry certEntry = pkcs12Store.GetCertificate(alias);
            if (certEntry != null)
            {
                Org.BouncyCastle.X509.X509Certificate cert = certEntry.Certificate;
                publicKey = cert.GetPublicKey();
                string txtPublicKey = Hex.ToHexString(((ECPublicKeyParameters)publicKey).Q.GetEncoded(false));
                Console.WriteLine("公钥 16进制字符串:" + txtPublicKey);
                Console.WriteLine("txtPublicKey 长度:" + txtPublicKey.Length);
            }

            #endregion

            #region 从AsymmetricKeyEntry拿私钥

            AsymmetricKeyParameter privateKey = null;
            AsymmetricKeyEntry mypri1 = pkcs12Store.GetKey(alias);
            if (mypri1 != null)
            {
                privateKey = mypri1.Key;
                Console.WriteLine("privateKey.IsPrivate:" + privateKey.IsPrivate);
                string txtPrivateKey = Hex.ToHexString(((ECPrivateKeyParameters)privateKey).D.ToByteArray());
                Console.WriteLine("私钥 16进制字符串:" + txtPrivateKey);
                Console.WriteLine("txtPrivateKey 长度:" + txtPrivateKey.Length);
            }

            #endregion

            #region 测试下私钥签名

            byte[] userId = Encoding.UTF8.GetBytes("1234567812345678");
            byte[] msg = System.Text.Encoding.UTF8.GetBytes("hello ,");
            byte[] sig = GmUtil.SignSm3WithSm2(msg, userId, privateKey);
            string txtSign = Convert.ToBase64String(sig);
            Console.WriteLine("签名BASE64:" + txtSign);

            #endregion

            //
            // stream.Dispose(); // 注意释放
            // reader.TryDecrypt("ASDF12345", out var privateKey);
            // var builder = WinFormiumApp.CreateBuilder();
            // builder.UseWinFormiumApp<MainForm>();
            // builder.UseEmbeddedBrowser();
            // builder.UseSingleApplicationInstanceMode(handler =>
            // {
            //     var retval = MessageBox.Show($"�Ѿ���һ��ʵ����������:{handler.ProcessId}��\r\n�Ƿ���������壿", "����ģʽ������", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            //     if (retval == DialogResult.Yes)
            //     {
            //         handler.ActiveRunningInstance();
            //     }
            // });
            // builder.UseDevToolsMenu();
            // var app = builder.Build();
            // app.Run();
        }
    }
}